
package com.borqs.music.ui.bpc;

import static com.borqs.music.http.RequestMethod.GET;
import static com.borqs.music.util.AccountUtils.BORQS_ACCOUNT_OPTIONS_KEY_SESSION;
import static com.borqs.music.util.MusicConf.ReqParam.USERID;
import static com.borqs.music.util.MusicConf.ReqType.MUSIC_COUNT_OF_USER_SONG;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.borqs.music.R;
import com.borqs.music.executor.MusicExecutor;
import com.borqs.music.executor.Task;
import com.borqs.music.executor.TaskAdapter;
import com.borqs.music.executor.TaskListener;
import com.borqs.music.http.HttpRequest;
import com.borqs.music.http.HttpResponse;
import com.borqs.music.tasks.SongTask;
import com.borqs.music.ui.online.OnlineTrackBrowserActivity;
import com.borqs.music.util.AccountUtils;
import com.borqs.music.util.BpcApiUtils;
import com.borqs.music.util.MusicConf;

public class ShareSourceReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(final Context context, Intent intent) {
        long userId = intent.getLongExtra(BpcApiUtils.User.USER_ID, 0);

        if (0 == userId) {
            String id = AccountUtils.getAccountId(context);
            try {
                userId = Long.parseLong(id);
            } catch (NumberFormatException ex) {
                ex.printStackTrace();
            }
        }

        TaskListener listener = new TaskAdapter() {
            @Override
            public void onDone(Task task) {
                Integer count = (Integer) task.getResult();
                if (count != null) {
                    notifyBPC(context, count);
                } else {
                    notifyBPC(context, 0);
                }
            }

            @Override
            public void onFail(Task task) {
                notifyBPC(context, 0);
            }
        };

        String ticket = AccountUtils.getUserData(context, BORQS_ACCOUNT_OPTIONS_KEY_SESSION);
        GetSharedCountTask task = new GetSharedCountTask(listener, ticket, userId);
        MusicExecutor.executeTask(task);
    }

    void notifyBPC(Context context, int count) {
        Intent intent = new Intent(BpcApiUtils.Action.SHARE_SOURCE_RESULT);
        Bundle bundle = new Bundle();
        bundle.putString(BpcApiUtils.Result.PACKAGE_NAME, context.getPackageName());
        bundle.putString(BpcApiUtils.Result.SOURCE_LABEL,
                context.getString(R.string.stream_shared_musics));
        bundle.putInt(BpcApiUtils.Result.SOURCE_COUNT, count);
        bundle.putString(BpcApiUtils.Result.CALLBACK_SCHEME, OnlineTrackBrowserActivity.SCHEME);
        intent.putExtra(BpcApiUtils.Action.SHARE_SOURCE_RESULT, bundle);

        context.sendBroadcast(intent);
    }
}

class GetSharedCountTask extends Task {
    private long mUserId;
    private String mTicket;
    private String mFileType = "mp3";

    public GetSharedCountTask(TaskListener listener, String ticket, long userId) {
        super(listener);
        mTicket = ticket;
        mUserId = userId;
    }

    @Override
    public HttpRequest prepareRequest() {
        Map<String, String> params = new HashMap<String, String>();
        params.put(USERID, String.valueOf(mUserId));

        params.put("filetype", mFileType);
        
        String url = MusicConf.getUrl(MUSIC_COUNT_OF_USER_SONG);
        HttpRequest req = new HttpRequest(GET, url, mTicket, params);
        return req;
    }

    @Override
    public Object handleResponse(HttpResponse resp) throws Exception {
        checkResponse(resp);
        JSONObject jObj = resp.asJSONObject();
        return jObj.optInt("result", 0);
    }
}
